package com.roadmap.ajax.handler;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.directwebremoting.WebContextFactory;

import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.BaseEntity;
import com.roadmap.common.RdcLogger;
import com.roadmap.exception.ActionNotSupportedException;
import com.roadmap.exception.EntityNotFoundException;
import com.roadmap.exception.InvalidActionException;
import com.roadmap.exception.InvalidQueryException;
import com.roadmap.exception.InvalidStatusException;
import com.roadmap.exception.InvalidValueException;
import com.roadmap.exception.NotAuthorizedException;
import com.roadmap.exception.NotMatchedException;
import com.roadmap.exception.NullObjectException;
import com.roadmap.exception.ServerException;
import com.roadmap.struts.action.ActionConst;

@Deprecated
public class LogicHandler implements ILogicHandler {
	private final static Logger _log = RdcLogger.getLogger(Logger.class);
	private final HttpSession _session = WebContextFactory.get().getSession();
	private IManager _bizManager = null;
	private final String _managerType;
	private String _ownerId; // the user of the handler
	private String _parentId; // the parent of the business manager

	public LogicHandler() {
		this._managerType = null;
	}

	public LogicHandler(String type) {
		this._managerType = type;
	}

	/** get the HTTP session */
	protected final HttpSession getSession() {
		return _session;
	}

	/** get the user id stored in session */
	protected final String getUIDInSession() {
		return (String) getSession().getAttribute(
				ActionConst.Aspirant_Id_In_Session);
	}

	/**
	 * get a business manager
	 * 
	 * @throws ServerException
	 * @throws NullObjectException
	 */
	protected final IManager getManager()
			throws InvalidValueException, ServerException {
		if (_bizManager == null) {
			_bizManager = ManagerFactory.getInstance(_managerType,
					getUIDInSession(), getOwnerId());
		}
		return _bizManager;
	}

	/** get the owner of the handler */
	public String getOwnerId() {
		if (_ownerId == null) {
			_ownerId = getUIDInSession();
		}
		return _ownerId;
	}

	public void setOwnerId(String id) {
		_ownerId = id;
	}

	public String getParentId() {
		return _parentId;
	}

	public void setParentId(String id) {
		_parentId = id;
	}

	// create a new entry in database
	public void create(BaseEntity entry) throws NullObjectException,
			InvalidActionException, InvalidValueException, ServerException,
			EntityNotFoundException, ActionNotSupportedException,
			InvalidStatusException, NotAuthorizedException {
		IManager mgr = getManager();
		if (mgr != null) {
			mgr.create(entry);
		}
		else {
			String msg = "failed to get an available business mananger.";
			NullObjectException e = new NullObjectException();
			_log.error(msg, e);
			throw e;
		}
	}

	public void delete(String entityId) throws ServerException,
			InvalidQueryException, InvalidValueException,
			InvalidActionException, EntityNotFoundException,
			NotAuthorizedException, ActionNotSupportedException {
		IManager mgr = getManager();
		if (mgr != null) {
			mgr.delete(entityId);
		}
		else {
			String msg = "failed to get an available business mananger.";
			NullObjectException e = new NullObjectException();
			_log.error(msg, e);
			throw e;
		}
	}

	public void edit(BaseEntity entry) throws NullObjectException,
			InvalidActionException, InvalidValueException, ServerException,
			NotMatchedException {
		IManager mgr = getManager();
		if (mgr != null) {
			mgr.edit(entry);
		}
		else {
			String msg = "failed to get an available business mananger.";
			NullObjectException e = new NullObjectException();
			_log.error(msg, e);
			throw e;
		}
	}

	public Object complete(String entityId) throws ServerException,
			InvalidValueException, EntityNotFoundException,
			InvalidActionException, NotAuthorizedException,
			ActionNotSupportedException, InvalidStatusException {
		IManager mgr = getManager();
		if (mgr != null) {
			return mgr.complete(entityId);
		}
		else {
			String msg = "failed to get an available business mananger.";
			NullObjectException e = new NullObjectException();
			_log.error(msg, e);
			throw e;
		}
	}

	public Object cancel(String entityId) throws ServerException,
			InvalidValueException, EntityNotFoundException,
			InvalidActionException, NotAuthorizedException,
			ActionNotSupportedException {
		IManager mgr = getManager();
		if (mgr != null) {
			return mgr.cancel(entityId);
		}
		else {
			String msg = "failed to get an available business mananger.";
			NullObjectException e = new NullObjectException();
			_log.error(msg, e);
			throw e;
		}
	}

}
